# -*- coding: utf-8 -*-
# !/usr/bin/python3
import sys
import os
import json
import time

import pytest
import module.globals as gbl
from log_helper import logger
from pathlib import Path
import yaml
from service.login.login import LoginClass

base_path = os.path.dirname(os.path.abspath(__file__))
parent_path = os.path.dirname(base_path)
sys.path.append(base_path)
sys.path.append(parent_path)

def readConfig(env, file_path):
    # 读取配置文件
    with open(file_path, encoding='UTF-8') as f:
        data = yaml.safe_load(f)

    return data[env]

@pytest.fixture(scope='session', autouse=True)
def login_shop():
    # 配置文件路径
    settings_file = 'demo.yml'
    settings_file_path = Path(base_path).joinpath(settings_file)

    # 环境变量
    gbl.env = gbl.cmdopt
    gbl.env = 'test'

    env_config = readConfig(gbl.env, settings_file_path)
    logger().info(f"环境信息：{gbl.env}, 配置文件路径：{settings_file_path}")

    # 初始化登录对象
    login_obj = LoginClass(env_config["loca_domain"])
    # 执行登录并处理响应
    response = login_obj.opendemoapi()
    # 更新全局授权信息
    gbl.LOCA_DOMAIN = env_config["loca_domain"]
    gbl.global_authorization_dic = {"Authorization": response['token']}


@pytest.fixture(autouse=True)
def delay_between_tests():
    yield  # 此处是测试用例的执行
    time.sleep(0.5)  # 每条用例执行后等待X
